package com.huacai.restaurant.service.impl;

import java.util.List;
import com.huacai.common.utils.DateUtils;
import com.huacai.restaurant.domain.Restaurant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huacai.restaurant.mapper.RestaurantMapper;
import com.huacai.restaurant.service.IRestaurantService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 餐厅信息Service业务层处理
 *
 * @author laq
 * @date 2025-06-17
 */
@Service
public class RestaurantServiceImpl implements IRestaurantService
{
    @Resource
    private RestaurantMapper restaurantMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询餐厅信息
     *
     * @param id 餐厅信息主键
     * @return 餐厅信息
     */
    @Override
    public Restaurant selectRestaurantById(Long id)
    {
        return restaurantMapper.selectRestaurantById(id);
    }

    /**
     * 查询餐厅信息列表
     *
     * @param restaurant 餐厅信息
     * @return 餐厅信息
     */
    @Override
    public List<Restaurant> selectRestaurantList(Restaurant restaurant)
    {
        return restaurantMapper.selectRestaurantList(restaurant);
    }

    /**
     * 新增餐厅信息
     *
     * @param restaurant 餐厅信息
     * @return 结果
     */
    @Override
    public int insertRestaurant(Restaurant restaurant)
    {
        restaurant.setCreateTime(DateUtils.getNowDate());
        return restaurantMapper.insertRestaurant(restaurant);
    }

    /**
     * 批量新增餐厅信息
     *
     * @param restaurants 餐厅信息List
     * @return 结果
     */
    @Override
    public int batchInsertRestaurant(List<Restaurant> restaurants)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(restaurants)) {
            try {
                for (int i = 0; i < restaurants.size(); i++) {
                    int row = restaurantMapper.insertRestaurant(restaurants.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == restaurants.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改餐厅信息
     *
     * @param restaurant 餐厅信息
     * @return 结果
     */
    @Override
    public int updateRestaurant(Restaurant restaurant)
    {
        restaurant.setUpdateTime(DateUtils.getNowDate());
        return restaurantMapper.updateRestaurant(restaurant);
    }

    /**
     * 批量删除餐厅信息
     *
     * @param ids 需要删除的餐厅信息主键
     * @return 结果
     */
    @Override
    public int deleteRestaurantByIds(Long[] ids)
    {
        return restaurantMapper.deleteRestaurantByIds(ids);
    }

    /**
     * 删除餐厅信息信息
     *
     * @param id 餐厅信息主键
     * @return 结果
     */
    @Override
    public int deleteRestaurantById(Long id)
    {
        return restaurantMapper.deleteRestaurantById(id);
    }

    @Override
    public List<Restaurant> selectRestaurantAllList(Restaurant restaurant) {
        return restaurantMapper.selectRestaurantList(restaurant);
    }
}
